/*
 * gpiochip0 [GPIOA] (16 lines)
 * gpiochip1 [GPIOB] (16 lines)
 * gpiochip10 [GPIOK] (8 lines)
 * gpiochip11 [GPIOZ] (8 lines)
 * gpiochip2 [GPIOC] (16 lines)
 * gpiochip3 [GPIOD] (16 lines)
 * gpiochip4 [GPIOE] (16 lines)
 * gpiochip5 [GPIOF] (16 lines)
 * gpiochip6 [GPIOG] (16 lines)
 * gpiochip7 [GPIOH] (16 lines)
 * gpiochip8 [GPIOI] (16 lines)
 * gpiochip9 [GPIOJ] (16 lines)
 */

#include <gpiod.h>
#include <errno.h>
#include <stdio.h>

int gpio_set_value(const char *gpio_chip, unsigned int line_num, int value) {
    struct gpiod_chip *chip;
    struct gpiod_line *line;
    int i, ret;

    chip = gpiod_chip_open_by_name(gpio_chip);
    if (!chip)
    {
        perror("Open chip failed\n");
        goto end;
    }

    line = gpiod_chip_get_line(chip, line_num);
    if (!line)
    {
        perror("Get line failed\n");
        goto close_chip;
    }

    ret = gpiod_line_request_output(line, NULL, value);
    if (ret < 0)
    {
        perror("Request line as output failed\n");
        goto release_line;
    }

release_line:
    gpiod_line_release(line);
close_chip:
    gpiod_chip_close(chip);
end:
    return 0;
}
